<?php

namespace App\Http\Controllers\Biz;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;

use App\Repositories\Eloquent\Mgt\UserRepository as User;
use App\Repositories\Eloquent\Biz\GiftRepository as Gift;

class PointController extends Controller
{
    protected $user, $gift;

    public function __construct(User $user, Gift $gift)
    {
        parent::__construct();

        $this->gift = $gift;

        $this->user = $user;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $gifts = $this->gift->index($request->input('order'))->get();

        return view($this->view, compact('gifts'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view($this->view);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        return $this->gift->store($request);
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $gift = $this->gift->show($id);

        if ($gift instanceof JsonResponse && $gift->getStatusCode() === 404) {
            return abort('404');
        }

        return view($this->view, compact('gift'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        return $this->gift->renew($request, $id);
    }


    public function list(Request $request)
    {
        $user = array();
        
        $query = $request->input('query');

        if (isset($query)) {

            $user = $this->user->show($query);

            if ($user instanceof JsonResponse && $user->getStatusCode() === 404) {
                return abort('404');
            }

            $user = $this->user->transform($this->act, $user);
            
        }

        $order = $request->input('order');

        $gifts = $this->gift->index($order, 1)->get();

        $paging = round(count($gifts)/3);

        return view($this->view, compact('user', 'paging','gifts', 'order', 'query'));
    }


    /**
     * 兑换礼品
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function exchange(Request $request)
    {
        $request->activity = $this->act;
        
        return $this->user->exchange($request);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
